<?php
    defined('M2_MICRO') or die('Direct Access to this location is not allowed.');

    /**
      * Import Controller class
      * @name $importController
      * @package M2 Micro Framework
      * @subpackage Modules
      * @author Alexander Chaika
      */
    class ImportController extends Controller {

        /**
          * Dispatch action which call action method
          * and setup default module options
          * @return mixed $result
          */
        public function dispatch() {
            // route task
            $task = System::getInstance()->getCmd('task', 'pakodan');

            $options = array();
            $method = $task.'Action';
            if (is_callable(array($this, $method))) {
                return $this->$method($options);
            } else {
                return $this->indexAction($options);
            }
        }

        /**
          * Default module action
          * @return mixed $result
          */
        private function indexAction($options) {
            // route default action
            return $this->pakodanAction($options);
        }

        /**
          * Action for process PAKODAN feeds
          * @param array $options
          * @return mixed $result
          */
        private function pakodanAction($options) {
            // get feed value
            $options['feed'] = System::getInstance()->getCmd('feed', 'sale');
            
            // proccess called feed
            switch($options['feed']) {
                case 'sale':
                default:
                    // import objects for Sale
                    $options['data-source'] = 'http://realt.by/typo3conf/ext/uedb_core/getrecords.php?login[user]=pakodan&login[pass]=4dfh3495gs&config[tableid]=1&config[type]=1&config[page]=1&config[records-per-page]=20';
                    $options['xslt-shema'] = 'pakodan-sale.xsl';
                    break;
                case 'house':
                    // import objects for Sale
                    $options['data-source'] = 'http://realt.by/typo3conf/ext/uedb_core/getrecords.php?login[user]=pakodan&login[pass]=4dfh3495gs&config[tableid]=4&config[type]=1&config[page]=1&config[records-per-page]=20';
                    $options['xslt-shema'] = 'pakodan-house.xsl';
                    break;
                case 'trade':
                    // import objects for Sale
                    $options['data-source'] = 'http://realt.by/typo3conf/ext/uedb_core/getrecords.php?login[user]=pakodan&login[pass]=4dfh3495gs&config[tableid]=3&config[type]=1&config[page]=1&config[records-per-page]=20';
                    $options['xslt-shema'] = 'pakodan-trade.xsl';
                    break;
                case 'rent':
                    // import objects for Rent
                    $options['data-source'] = 'http://realt.by/typo3conf/ext/uedb_core/getrecords.php?login[user]=pakodan&login[pass]=4dfh3495gs&config[tableid]=2001&config[type]=1&config[page]=1&config[records-per-page]=20';
                    $options['xslt-shema'] = 'pakodan-rent.xsl';
                    break;
                case 'rhouse':
                    // import objects for Rent house
                    $options['data-source'] = 'http://realt.by/typo3conf/ext/uedb_core/getrecords.php?login[user]=pakodan&login[pass]=4dfh3495gs&config[tableid]=2002&config[type]=1&config[page]=1&config[records-per-page]=20';
                    $options['xslt-shema'] = 'pakodan-rhouse.xsl';
                    break;
            }

            // process import
            return $this->importData($options);
        }

        /**
          * Method which import data for apropriate feed
          * @param array $options
          * @return mixed $result
          */
        private function importData($options) {
            // get config
            $config = System::getInstance()->getConfig();

            // Get XML data from realt.by
            try {
                // get XML document & XSLT shema
                $xml = simplexml_load_file($options['data-source']);
                $xsl = simplexml_load_file(convertOSPath($config['doc_root'] . $config['xml_path'] . DS . $options['xslt-shema']));

                // create processor and transform export data to gisa shema
                $xslt = new XSLTProcessor();
                $xslt->importStylesheet($xsl);

                $options['data'] = $xslt->transformToXml($xml);
                if (empty($options['data']) && !$options['data']) {
                    return $this->throwMessage('Could not process XSLT Transformation for "'.$options['xslt-shema'].'" feed');
                } else {
                    $options['state'] = 'success';
                }
            } catch(Exception $e) {
                return $this->throwMessage($e->getMessage());
            }

            // Import data to DB
            return $this->model->importFromXML($options);
        }
    }
?>